﻿/* 
 * 自定义树形控件
 */
(function ($) {
    $.fn.dropdowntree = function (option) {
        /// <summary>
        /// 树形下拉控件初始化,基于dropdowns-enhancement控件
        /// </summary>
        /// <param name="option">配置项:<br/>
        /// defaultval:默认的value，默认为'';<br/>
        /// data:静态数据json，默认为{};
        /// value:控件选择的value值在json中的字段名称，默认为'value';<br/>
        /// text:控件显示的text值在json中的字段名称，默认为'text';
        /// showall:是否显示“全部”按钮，默认为true;<br/>
        /// allvalue:“全部”选项的value，配合showall使用，默认为'';
        /// alltext:“全部”选项的text，配合showall使用，默认为'ALL';<br/>
        /// sons:子节点在json中的字段名称，默认为'sons';
        /// onlyselleaf:是否仅可以选择叶子(无分支)节点，默认为false;
        ///</param>
        $.fn.dropdowntree.div = $(this);
        $.fn.dropdowntree.options = $(this).find(".dropdown-menu");
        var opt = {
            defaultval: '',
            data: {},
            value: "value",
            text: "text",
            showall: true,
            allvalue: "",
            alltext: "ALL",
            sons: "nodes",
            onlyselleaf: false
        };
        $.extend(opt.ajax, option.ajax);
        option.ajax = opt.ajax;
        $.extend(opt, option);
        if (!opt.value) {
            alert("Please enter a value !");
            return;
        }
        if (!opt.text) {
            alert("Please enter a text !");
            return;
        }
        $.fn.dropdowntree.options.empty();
        if (opt.showall) {
            $(this).find(".showbtn").text(opt.alltext);
            re = new RegExp('"', 'g');
            opt.allvalue = opt.allvalue.replace(re, '\\"');
            $.fn.dropdowntree.options.append('<li><a href="javascript:void(0);" class="leaf" data-key="' + opt.allvalue + '">' + opt.alltext + '</a></li>');
            $.fn.dropdowntree.options.append('<li class="divider"></li>');
        }
        var html = "";
        for (var i = 0; i < opt.data.length; i++) {
            html += $.fn.dropdowntree.getli(opt.data[i], opt.value, opt.text, opt.sons, opt.onlyselleaf);
        }
        $.fn.dropdowntree.options.append(html);

        var leafs = $(".leaf");
        leafs.each(function (i, e) {
            $(e).click(function () {
                $.fn.dropdowntree.setValue($(e).attr("data-key"));
            });
        });
        if (opt.defaultval) {
            $.fn.dropdowntree.setValue(opt.defaultval);
        }
    }
    $.fn.dropdowntree.count = 0;
    $.fn.dropdowntree.value = "";
    $.fn.dropdowntree.text = "";
    $.fn.dropdowntree.div;
    $.fn.dropdowntree.options;
    $.fn.dropdowntree.getli = function (item, key, value, sons, onlyselleaf) {
        $.fn.dropdowntree.count++;
        var html = "";
        var keystr = item[key];
        if (!keystr) {
            keystr = $.fn.dropdowntree.count;
        } else {
            re = new RegExp('"', 'g');
            keystr = keystr.toString().replace(re, '&quot;');
        }
        var valuestr = item[value];
        var sonArry = item[sons];
        if (sonArry && sonArry.length > 0) {
            var classstr = "";
            if (!onlyselleaf) {
                classstr = "leaf";
            }
            html += "<li class=\"dropdown-submenu\"><a href=\"javascript:void(0);\" data-toggle=\"dropdown\" class=\"" + classstr + "\" data-key=\"" + keystr + "\">" + valuestr + "</a>";
            html += "<ul class=\"dropdown-menu\">";
            for (var i = 0; i < sonArry.length; i++) {
                html += $.fn.dropdowntree.getli(sonArry[i], key, value, sons, onlyselleaf);
            }
            html += "</ul>";
            html += "</li>";
        } else {
            html += "<li><a href=\"javascript:void(0);\" class=\"leaf\" data-key=\"" + keystr + "\">" + valuestr + "</a></li>";
        }
        return html;
    }
    $.fn.dropdowntree.setValue = function (itemid) {
        /// <summary>
        /// 树形控件设置值
        /// </summary>
        /// <param name="itemid">要设置的value值</param>
        $.fn.dropdowntree.value = itemid;
        re = new RegExp("'", 'g');
        itemid = itemid.toString().replace(re, "\\'");
        $.fn.dropdowntree.text = $.fn.dropdowntree.options.find("a.leaf[data-key='" + itemid + "']").html();
        $.fn.dropdowntree.div.find(".showbtn").text($.fn.dropdowntree.text);
    }
    $.fn.dropdowntree.getValue = function () {
        /// <summary>
        /// 获取控件当前选中的value值
        /// </summary>
        return $.fn.dropdowntree.value;
    }
    $.fn.dropdowntree.getText = function () {
        /// <summary>
        /// 获取控件当前选中的text值
        /// </summary>
        return $.fn.dropdowntree.text;
    }
})(jQuery);